package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.pojo.TbSpecificationOptionExample;
import com.dongdong.service.SpecificationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private TbSpecificationMapper tbSpecificationMapper;

    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> getSpecifications() {
        return tbSpecificationMapper.selectByExample(null);
    }

    @Override
    public List<TbSpecification> getSpecificationList(String specName) {
        return tbSpecificationMapper.getSpecificationList(specName);
    }


    @Override
    public int SaveSpecification(TbSpecification tbSpecification, List<TbSpecificationOption> options) {

        tbSpecificationMapper.SaveSpecification(tbSpecification);
        System.out.println(tbSpecification.getId());
        //Long id = tbSpecification.getId();/
        tbSpecificationOptionMapper.insertBatch(options,tbSpecification.getId());
        return 1;
    }

    @Override
    public TbSpecification toUpdate(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        List<TbSpecificationOption> list = tbSpecificationOptionMapper.getSpects(id);
        tbSpecification.setList(list);
        return tbSpecification;
    }

    @Override
    public int updateSpecification(TbSpecification tbSpecification, List<TbSpecificationOption> list) {
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(example);
        tbSpecificationOptionMapper.insertBatch(list,tbSpecification.getId());
        return 1;
    }

    @Override
    public int deleteBatch(Long[] ids) {
        tbSpecificationMapper.deleteBatch(ids);
        tbSpecificationOptionMapper.deleteBatchByIds(ids);
        return 0;
    }
}
